#include "stdafx.h"
#include "Kingdom.h"
#include "General.h"
#include "Army.h"
#include "City.h"

sgObject * sgKingdom::Creator()
{
    return new sgKingdom;
}

bool sgKingdom::m_creatorRegisted = CObjectManager::Instance().RegisteCreator(OBJ_KINGDOM, sgKingdom::Creator );


sgKingdom::sgKingdom()
{
    m_color = 0;
    m_king = NULL;
    m_capital = NULL;
}

sgKingdom::~sgKingdom()
{

}

void sgKingdom::Update( int diff )
{

}

void sgKingdom::Render()
{

}

void sgKingdom::SetKing( sgGeneral *king )
{
    m_king = king;
}

sgGeneral * sgKingdom::GetKing()
{
    return m_king;
}

int sgKingdom::GetColor()
{
    return m_color;
}

void sgKingdom::SetColor( int color )
{
    m_color = color;
}

void sgKingdom::SetName( const std::string &name )
{
    m_name = name;
}

std::string & sgKingdom::GetName()
{
    return m_name;
}

GeneralMap & sgKingdom::GetGenerals()
{
    return m_generals;
}

void sgKingdom::AddGeneral( sgGeneral *g )
{
    if( GetGeneral(g->GetID()) != NULL )
    {
        return;
    }
    m_generals.insert( std::make_pair(g->GetID(), g) );
}

sgGeneral * sgKingdom::GetGeneral( int id )
{
    GeneralMap::iterator iter = m_generals.find(id);
    if( iter != m_generals.end() )
    {
        return iter->second;
    }
    return NULL;
}

void sgKingdom::RemoveGeneral( int id )
{
    GeneralMap::iterator iter = m_generals.find(id);
    if( iter != m_generals.end() )
    {
        m_generals.erase( iter );
    }
}

void sgKingdom::AddArmy( sgArmy *a )
{
    m_armies[a->GetID()] = a;
}

sgArmy * sgKingdom::GetArmy( int id )
{
    ArmyMap::iterator iter = m_armies.find(id);
    if( iter != m_armies.end() )
    {
        return iter->second;
    }
    return NULL;
}

void sgKingdom::AddCity( sgCity *c )
{
    m_cities[c->GetID()] = c;
}

sgCity * sgKingdom::GetCity( int id )
{
    CityMap::iterator iter = m_cities.find(id);
    if( iter != m_cities.end() )
    {
        return iter->second;
    }
    return NULL;
}

ArmyMap & sgKingdom::GetArmies()
{
    return m_armies;
}

CityMap & sgKingdom::GetCities()
{
    return m_cities;
}

void sgKingdom::SetCapital( sgCity *c )
{
    m_capital = c;
}

sgCity * sgKingdom::GetCapital()
{
    return m_capital;
}

void sgKingdom::Serialize( CSerialize& s )
{
    sgObject::Serialize(s);
    s&MAKE_PAIR(m_name, "name");
    std::vector<int> cities,armies,generals;
    int kingid = -1;
    int captalid = -1;
    if( s.IsSaving() )
    {
        if( m_king )
        {
            kingid = m_king->GetID();
            s&MAKE_PAIR(kingid, "king");
        }
        if( m_capital )
        {
            captalid = m_capital->GetID();
            s&MAKE_PAIR(captalid, "capital");
        }
        for( CityMap::iterator iter = m_cities.begin(); iter != m_cities.end(); iter++ )
        {
            cities.push_back( iter->first );
        }
        std::string citystr = VectorToString( cities );
        s&MAKE_PAIR(citystr, "city");
        for( GeneralMap::iterator iter = m_generals.begin(); iter != m_generals.end(); iter++ )
        {
            generals.push_back( iter->first );
        }
        std::string generalstr = VectorToString( generals );
        s&MAKE_PAIR(generalstr, "general");
        for( ArmyMap::iterator iter = m_armies.begin(); iter != m_armies.end(); iter++ )
        {
            armies.push_back( iter->first );
        }
        std::string armystr = VectorToString( armies );
        s&MAKE_PAIR(armystr, "army");
    }
    else if( s.IsLoading() )
    {
        s&MAKE_PAIR(kingid, "king");
        m_king = (sgGeneral *)CObjectManager::Instance().GetObject(kingid);
        s&MAKE_PAIR(captalid, "capital");
        m_capital = (sgCity *)CObjectManager::Instance().GetObject(captalid);
        std::string citystr,generalstr,armystr;
        s&MAKE_PAIR(citystr, "city");
        s&MAKE_PAIR(generalstr, "general");
        s&MAKE_PAIR(armystr, "army");
        StringToVector(citystr,cities);
        StringToVector(armystr,armies);
        StringToVector(generalstr,generals);
        for( std::vector<int>::iterator iter = cities.begin(); iter != cities.end(); iter++ )
        {
            sgCity *city = (sgCity *)CObjectManager::Instance().GetObject(*iter);
            if( city )
            {
                AddCity(city);
            }
        }
        for( std::vector<int>::iterator iter = armies.begin(); iter != armies.end(); iter++ )
        {
            sgArmy *army = (sgArmy *)CObjectManager::Instance().GetObject(*iter);
            if( army )
            {
                AddArmy(army);
            }
        }
        for( std::vector<int>::iterator iter = generals.begin(); iter != generals.end(); iter++ )
        {
            sgGeneral *general = (sgGeneral *)CObjectManager::Instance().GetObject(*iter);
            if( general )
            {
                AddGeneral(general);
            }
        }
    }
}

